package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import com.cskaoyan.exception.UnLoginException;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.utils.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Description
 * 小程序优惠卷
 *
 * @author Zhangfei
 * @date Created on 2021/10/21
 */
@RestController
@RequestMapping("/wx/coupon")
public class WXCouponController {

    @Autowired
    CouponService couponService;


    @PostMapping("/receive")
    public BaseRespVo receive(@RequestBody Map map) throws UnLoginException {
        Integer couponId = (Integer) map.get("couponId");
        //获得登陆主体
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) throw new UnLoginException("未登录,请登录");
        //传入id
        Integer userId = user.getId();
        Integer count = couponService.receive(couponId, userId);
        if (count == Constant.SUCCESS) {
            return BaseRespVo.ok();
        }

        if (count == Constant.COUPON_RECEIVED) {
            return BaseRespVo.fail(Constant.COUPON_RECEIVED, "已领取过优惠卷");
        }

        if (count == Constant.COUPON_ONLY_EXCHANGE) {
            return BaseRespVo.fail(Constant.COUPON_RECEIVED, "仅供兑换码兑换");
        }

        return BaseRespVo.fail("兑换失败");
    }

    /**
     * 根据用户id和优惠卷状态以及分页信息查询优惠卷详情
     *
     * @param param
     * @return
     */
    @GetMapping("/list")
    public BaseRespVo list(BaseParam param) throws UnLoginException {

        BasePageData mylist = couponService.mylist(null, 0, param);

        return BaseRespVo.ok(mylist);
    }


    /**
     * 根据用户id和优惠卷状态以及分页信息查询优惠卷详情
     *
     * @param status
     * @param param
     * @return
     */
    @GetMapping("/mylist")
    public BaseRespVo mylist(Integer status, BaseParam param) throws UnLoginException {
        // 获得登陆主体
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) throw new UnLoginException("未登录,请登录");
        // 传入id
        Integer userId = user.getId();

        BasePageData mylist = couponService.mylist(userId, status, param);

        return BaseRespVo.ok(mylist);
    }

    /**
     * 兑换优惠卷
     *
     * @param
     * @return
     */
    @PostMapping("/exchange")
    public BaseRespVo exchange(@RequestBody Map map) throws UnLoginException {
        // 获得登陆主体
        String code = (String) map.get("code");
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        // 传入id
        // if(user==null) throw new UnLoginException("未登录,请登录");
        Integer userId = user.getId();
        Integer count = couponService.exchange(userId, code);

        //根据结果响应相应数据
        if (count == Constant.SUCCESS) {
            return BaseRespVo.ok();
        }

        if (count == Constant.COUPON_RECEIVED) {
            return BaseRespVo.fail(Constant.COUPON_RECEIVED, "已领取过优惠卷");
        }

        if (count == Constant.COUPON_CODE_FALUT) {
            return BaseRespVo.fail(Constant.COUPON_CODE_FALUT, "优惠券不正确");
        }

        return BaseRespVo.fail("兑换失败");
    }

    /**
     * 选择优惠卷，因为目前不加入团购故，暂时对于grouponRulesId不做处理
     *
     * @param cartId
     * @param
     * @return
     */
    @GetMapping("/selectlist")
    public BaseRespVo selectlist(Integer cartId, Integer grouponRulesId) throws UnLoginException {
        // 获得登陆主体
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) throw new UnLoginException("未登录,请登录");
        // 传入id
        Integer userId = user.getId();
        BasePageData data = couponService.selectlist(userId, cartId);
        return BaseRespVo.ok(data);
    }
}
